using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using RecruitmentAgency.Models.DataProviders;
using RecruitmentAgency.Models.DataTypes;
using RecruitmentAgency.Models.Utilities;

namespace RecruitmentAgency.Controllers
{
    /// <summary>
    /// The RecruitmentAgency.Controllers.JobController provides methods and properties useful to the Job page in the RecruitmentAgency web site
    /// </summary>
    public class JobController : BaseController
    {
        private List<string> jobSectors, jobTitles;

        /// <summary>
        /// Initialises a new RecruitmentAgency.Controllers.JobController instance
        /// </summary>
        public JobController() : base()
        {
            jobSectors = CommonMethods.AddSelectMessageToList(DataProvider.GetSectors().OrderBy(n => n).ToList());
            ViewData["sectors"] = new SelectList(jobSectors);
        }
        
        private IJobDataProvider JobDataProvider
        {
            get
            {
                return new JobDataProvider();
            }
        }

        /// <summary>
        /// This GET ViewJobs method returns a JobSearchData instance that contains one page of all Job results
        /// </summary>
        /// <param name="page">The page number of results required</param>
        /// <returns>A List of Job instances</returns>
        public ActionResult ViewJobs(int? page)
        {
            string jobSector = string.Empty;
            if (Request.QueryString["sector"] != null) jobSector = Request.QueryString["sector"];
            if (Request.QueryString["pageSize"] != null) PageSize = int.Parse(Request.QueryString["pageSize"]);
            jobSectors = CommonMethods.AddInputMessageToList(JobDataProvider.GetUniqueJobSectors(), string.Empty);
            ViewData["sectors"] = new SelectList(jobSectors);
            jobTitles = CommonMethods.AddInputMessageToList(JobDataProvider.GetUniqueJobTitles(), string.Empty);
            ViewData["pageSizes"] = new SelectList(DefaultPageSizes, PageSize);
            return View(JobDataProvider.SearchJobs(PageSize, page.HasValue ? page.Value : 1, string.Empty, string.Empty, string.Empty, jobSector));
        }

        /// <summary>
        /// This POST ViewJobs method returns a JobSearchData instance that contains one page of all Job results
        /// </summary>
        /// <param name="page">The page number of results required</param>
        /// <param name="searchData">The JobSearchData instance that holds the search parameters from the form</param>
        /// <returns>A List of Job instances</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ViewJobs(int? page, JobSearchData searchData)
        {
            jobSectors = CommonMethods.AddInputMessageToList(JobDataProvider.GetUniqueJobSectors(), string.Empty);
            ViewData["sectors"] = new SelectList(jobSectors);
            jobTitles = CommonMethods.AddInputMessageToList(JobDataProvider.GetUniqueJobTitles(), string.Empty);
            ViewData["pageSizes"] = new SelectList(DefaultPageSizes, PageSize);
            return View(JobDataProvider.SearchJobs(PageSize, page.HasValue ? page.Value : 1, searchData.JobTitle, searchData.JobDescription, searchData.ClosingDate, searchData.JobSector != "Please select an option from the list" ? searchData.JobSector : ""));
        }

        /// <summary>
        /// This GET ViewJob method returns a Job instance related to the input ID parameter
        /// </summary>
        /// <param name="id">The ID number of the job that is required</param>
        /// <returns>A Job instance</returns>
        public ActionResult ViewJob(int id)
        {
            return View(JobDataProvider.GetJob(id));            
        }

        #region Edit

        /// <summary>
        /// This GET Edit method returns the Job instance related to the input ID parameter
        /// </summary>
        /// <param name="id">The ID number of the job that is required</param>
        /// <returns>A Job instance</returns>
        public ActionResult Edit(int id)
        {
            return View(JobDataProvider.GetJob(id));
        }

        /// <summary>
        /// This POST Edit method returns a Job instance according to its Id
        /// </summary>
        /// <param name="job">The job instance</param>
        /// <returns>Returns a Job instance </returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Job job)
        {
            JobDataProvider.UpdateJob(job);
            return View(JobDataProvider.GetJob(job.JobId));
        }

        #endregion
        
        #region delete
        /// <summary>
        /// This GET Delete method returns a Job instance relating to the input ID parameter to be deleted
        /// </summary>
        /// <param name="id">The id of the job to delete</param>
        /// <returns>Returns a Job instance relating to the input ID parameter to be deleted</returns>
        public ActionResult Delete(int id)
        {
            return View(JobDataProvider.GetJob(id));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="personalDetails"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, PersonalDetail personalDetails)
        {
            JobDataProvider.DeleteJob(id);
            return RedirectToAction("ViewJobs");
        }

        #endregion

        #region Create

        /// <summary>
        /// This POST Create method creates add a new Job record in the database
        /// </summary>
        /// <param name="job">The job instance containing the job detailsto be added to the database</param>
        /// <returns>Redirect to the ViewJobs page</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "JobId")]Job job)
        {
            job.JobProviderId = JobProviderDataProvider.GetJobProviderByPersonId(CurrentPersonId).JobProviderId;
            JobDataProvider.AddJob(job);
            return RedirectToAction("ViewJobs");
        }


        /// <summary>
        /// This GET method direct to the Add new Job page
        /// </summary>        
        public ActionResult Create()
        {            
            return View();
        }

         #endregion

        /// <summary>
        /// Returns a RSS feed of Job data in ATOM1.0 format
        /// </summary>
        /// <returns>A RSS feed of Job data in ATOM1.0 format</returns>
        public ActionResult JobRssFeed()
        {
            return new RssFeedActionResult() { Feed = JobDataProvider.GetJobRssFeed(Request.Url.AbsoluteUri.Replace("JobRssFeed", string.Empty)) };
        }
    }
}